package com.store.controller;

import com.store.common.Result;
import com.store.service.PerformanceService;
import com.store.vo.PerformanceVO;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

/**
 * 业绩统计控制器
 */
@Slf4j
@RestController
@RequestMapping("/performance")
@RequiredArgsConstructor
public class PerformanceController {
    
    private final PerformanceService performanceService;
    
    /**
     * 获取员工业绩统计
     */
    @GetMapping("/employee/{employeeId}")
    public Result<PerformanceVO> getEmployeePerformance(
            @PathVariable Long employeeId,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate,
            HttpServletRequest httpRequest) {
        try {
            PerformanceVO performance = performanceService.getEmployeePerformance(employeeId, startDate, endDate);
            return Result.success(performance);
        } catch (Exception e) {
            log.error("获取员工业绩统计失败", e);
            return Result.error("获取员工业绩统计失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取员工总业绩
     */
    @GetMapping("/employee/{employeeId}/total")
    public Result<PerformanceVO> getEmployeeTotalPerformance(
            @PathVariable Long employeeId,
            HttpServletRequest httpRequest) {
        try {
            PerformanceVO performance = performanceService.getEmployeeTotalPerformance(employeeId);
            return Result.success(performance);
        } catch (Exception e) {
            log.error("获取员工总业绩失败", e);
            return Result.error("获取员工总业绩失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取员工本月业绩
     */
    @GetMapping("/employee/{employeeId}/month")
    public Result<PerformanceVO> getEmployeeMonthPerformance(
            @PathVariable Long employeeId,
            HttpServletRequest httpRequest) {
        try {
            PerformanceVO performance = performanceService.getEmployeeMonthPerformance(employeeId);
            return Result.success(performance);
        } catch (Exception e) {
            log.error("获取员工本月业绩失败", e);
            return Result.error("获取员工本月业绩失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取当前登录员工的业绩（用于员工端）
     */
    @GetMapping("/my")
    public Result<PerformanceVO> getMyPerformance(
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate,
            HttpServletRequest httpRequest) {
        try {
            Long employeeId = getUserId(httpRequest);
            PerformanceVO performance = performanceService.getEmployeePerformance(employeeId, startDate, endDate);
            return Result.success(performance);
        } catch (Exception e) {
            log.error("获取我的业绩失败", e);
            return Result.error("获取我的业绩失败：" + e.getMessage());
        }
    }
    
    /**
     * 从请求中获取用户ID
     */
    private Long getUserId(HttpServletRequest request) {
        Object userIdObj = request.getAttribute("userId");
        if (userIdObj == null) {
            throw new RuntimeException("未登录或登录已过期");
        }
        return Long.valueOf(userIdObj.toString());
    }
}

